package com.winit.common.sg;

import java.io.UnsupportedEncodingException;

/**
 * Created by wanglin on 2018/4/28.
 */
public class RegularUtil {
    public final static  String     FILESUFFIX=".java";
    public final static  String[]   BasicTypes ={"String","Date","int","Integer","boolean","long","Long","BigDecimal","Boolean","GoodsStatus","Timestamp","String[]","Long[]"};
    private final static String     getMethNameAndParameter="([\\s\\S]*)([ ])(\\S*)(\\()([\\s\\S]*)(\\))([\\s\\S]*)";
    private final static String     getServicePath="([\\s\\S]*)(dubbo://)([\\s\\S]*?)(/)([\\s\\S]*?)(\\?)([\\s\\S]*)";

    private final static String     getMethNames="([\\s\\S]*)(&methods=)([\\s\\S]*?)(&)([\\s\\S]*)";

    private final static String     getServiceName="([\\s\\S]*)(\\/)([\\s\\S]*)(\\.)([\\s\\S]*)";
    private final static String     getEntityPath="(\\S*)([ ]*)(\\S*)(\\;)";
    private final static String     getUpPath="(\\S*)(java/)(\\S*)(\\/)(\\S*)";
    public final static  String     isPathAll="(\\S*)(import)([ ]com)(\\S*)(\\*)(\\;)";
    public final static  String     getPathAll="(\\S*)(import)([ ])(\\S*)(\\*)(\\;)";
    private final static String     getExtend="([\\s\\S]*)(public)([\\s\\S]*)(extends)([ ]*)([a-zA-Z0-9]*)([ ]*)([\\s\\S]*)";
    private final static String     getServicePathSys="(\\S*)(\\/)(\\S*)(\\/)(\\S*)(spi)([\\s\\S]*)";
    private final static String     getListEntity="([\\s\\S]*)(\\<)([\\S\\s]*)(\\>)([\\s\\S]*)";

    public  final static String     isExtendT="([\\s\\S]*)([ ])(T)([\\s\\S]*)";
    private final static String     getExtendT="([\\s\\S]*)(extends)(\\s)([a-zA-Z0-9]*)([\\s\\S]*)";


    public static String getMethNameAndParameter(String str,String select){
        if(str.matches(getMethNameAndParameter)){
            str=str.replaceAll(getMethNameAndParameter, select);
        }else{
            return null;
        }
        return str;
    }

    public static String getExtendTStr(String str){
        return str.replaceAll(getExtendT, "$4");
    }

    public static String[] getMethNames(String str,String select){
        str= transcoding(str);
        if(str.matches(getMethNames)){
            str=str.replaceAll(getMethNames, select);
        }else{
            return null;
        }
        return str.split(",");
    }

    public static String getServicePath(String str,String select){
        str= transcoding(str);
        if(str.matches(getServicePath)){
            str=str.replaceAll(getServicePath, select);
        }else{
            return null;
        }
        return str.replace(".", "/");
    }

    public static String getServiceName(String str, String select){
        if(str.matches(getServiceName)){
            str=str.replaceAll(getServiceName, select);
        }else{
            return null;
        }
        return str;
    }

    public static String getServicePathSys(String str,String select){
        if(str.matches(getServicePathSys)){
            str=str.replaceAll(getServicePathSys, select);
        }else{
            return null;
        }
        return str;
    }

    public static String getEntityPath(String entity,String str,String select){
        if(str.matches("(\\s*)(import)([ ])(\\S*)(" + entity + ")(\\;)(\\s*)")){
            str=str.replaceAll(getEntityPath, select).replace(".", "/");
        }else{
            return null;
        }
        return str.trim();
    }

    public static String getUpPath(String str,String select){
        if(str.matches(getUpPath)){
            str=str.replaceAll(getUpPath, select);
        }else{
            return null;
        }
        return str;
    }

    public static String getExtend(String str,String select){
        if(str.matches(getExtend)){
            str=str.replaceAll(getExtend, select);
        }else{
            return null;
        }
        return str;
    }

    public static String getListEntity(String str,String select){
        if(str.matches(getListEntity)){
            str=str.replaceAll(getListEntity, select);
        }else{
            return null;
        }
        return str;
    }

    private static String transcoding(String str){
        try {
             str= java.net.URLDecoder.decode(str, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

}
